package model

type Page struct {
	PageSize int64  `json:"limit"`
	Offset   int64  `json:"offset"`
	Page     int64  `json:"page"`
	Total    int64  `json:"-"`
	Query    string `json:"-"`
	OrderBy  string `json:"orderBy"`
	Sort     string `json:"sort"`
}

type Result struct {
	Total       *int64      `json:"total,omitempty"`
	Pages       *int64      `json:"pages,omitempty"`
	CurrentPage *int64      `json:"currentPage,omitempty"`
	PageSize    *int64      `json:"pageSize,omitempty"`
	Results     interface{} `json:"result"`
	Code        int64       `json:"code"`
}

func NewResult(count int64, page *Page, results interface{}) Result {
	var result Result
	var pages int64

	if page != nil {
		result = Result{
			Total:       &count,
			CurrentPage: &page.Page,
			PageSize:    &page.PageSize,
			Pages:       &pages,
			Results:     results,
			Code:        0,
		}

		pc := count / page.PageSize
		result.Pages = &pc
		if count%page.PageSize > 0 {
			*(result.Pages) += 1
		}
	} else {
		result = Result{
			Results: results,
			Code:    0,
		}
	}

	return result
}
